package com.dg.river.core.util.js;

import android.app.Activity;
import android.content.Context;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import androidx.annotation.NonNull;

import com.amap.api.location.AMapLocation;
import com.dg.river.core.manager.UserInfoManager;
import com.dg.river.core.manager.UserInfoMethod;
import com.dg.river.core.util.L;
import com.dg.river.core.util.LocationUtils;
import com.dg.river.core.util.PermissionsCheckerUtils;
import com.dg.river.core.util.ScreenUtils;
import com.dg.river.core.util.Utils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Js交互的处理类
 */
public class JSInterface {

    private Context mContext;
    private Activity activity;

    // 网页控件
    private WebView webView;

    // JS交互类型
    private int ENUM_TYPE = 0;
    // 获取经纬度
    private final int TYPE_GET_LOCATION = 1001;
    private final String TYPE_GET_LOCATION_TEXT = "getLocation";
    // 获取船舶信息
    private final int TYPE_GET_SHIP_INFO = 1002;
    private final String TYPE_GET_SHIP_INFO_TEXT = "getShipInfo";
    // 获取状态栏高度
    private final int TYPE_GET_STATUS_BAR_HEIGHT = 1003;
    private final String TYPE_GET_STATUS_BAR_HEIGHT_TEXT = "getStatusBarHeight";

    public JSInterface(Context context, WebView webView) {
        this.mContext = context;
        this.activity = (Activity) context;
        this.webView = webView;
    }

    @JavascriptInterface
    public void showInfoFromJs() {

    }

    @JavascriptInterface
    public void showInfoFromJs(String value) {
        L.d("Js交互方法：" + value);
        if (TYPE_GET_LOCATION_TEXT.equals(value)) { // 传入经纬度、城市
            ENUM_TYPE = TYPE_GET_LOCATION;
        } else if (TYPE_GET_SHIP_INFO_TEXT.equals(value)) { // 获取船舶信息
            ENUM_TYPE = TYPE_GET_SHIP_INFO;
        } else if (TYPE_GET_STATUS_BAR_HEIGHT_TEXT.equals(value)) { // 获取状态栏高度
            ENUM_TYPE = TYPE_GET_STATUS_BAR_HEIGHT;
        }
        // 处理逻辑
        dealFunction();
    }

    /**
     * 处理逻辑
     */
    private void dealFunction() {
        switch (ENUM_TYPE) {
            case TYPE_GET_LOCATION:
                //检查权限
                if (!PermissionsCheckerUtils.checkLocationPermission(mContext)) {
                    XXPermissions.with(mContext)
                            .permission(Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION)
                            .request(new OnPermissionCallback() {

                                @Override
                                public void onGranted(@NonNull List<String> permissions, boolean all) {
                                    if (all) {
                                        // 获取定位信息
                                        LocationUtils.getInstance().getLocation(mContext, new LocationUtils.LocationCallBack() {
                                            @Override
                                            public void locationInfo(AMapLocation aMapLocation) {
                                                if (aMapLocation != null) {
                                                    Map<String, Object> locationMap = new HashMap<>();
                                                    locationMap.put("latitude", aMapLocation.getLatitude());
                                                    locationMap.put("longitude", aMapLocation.getLongitude());
                                                    locationMap.put("city", aMapLocation.getCity());
                                                    locationMap.put("cityCode", aMapLocation.getCityCode());
                                                    sendMsgToH5(locationMap, "getLocation");
                                                } else {
                                                    sendMsgToH5(null, "getLocation");
                                                }
                                            }

                                            @Override
                                            public void locationError() {
                                                sendMsgToH5(null, "getLocation");
                                            }
                                        });
                                    }
                                }

                                @Override
                                public void onDenied(@NonNull List<String> permissions, boolean never) {
                                    if (never) {
                                        sendMsgToH5(null, "getLocation");
                                    }
                                }
                            });
                } else {
                    // 传入经纬度、城市
                    LocationUtils.getInstance().getLocation(mContext, new LocationUtils.LocationCallBack() {
                        @Override
                        public void locationInfo(AMapLocation aMapLocation) {
                            if (aMapLocation != null) {
                                Map<String, Object> locationMap = new HashMap<>();
                                locationMap.put("latitude", aMapLocation.getLatitude());
                                locationMap.put("longitude", aMapLocation.getLongitude());
                                locationMap.put("city", aMapLocation.getCity());
                                locationMap.put("cityCode", aMapLocation.getCityCode());
                                sendMsgToH5(locationMap, "getLocation");
                            } else {
                                sendMsgToH5(null, "getLocation");
                            }
                        }

                        @Override
                        public void locationError() {
                            sendMsgToH5(null, "getLocation");
                        }
                    });
                }
                break;
            case TYPE_GET_SHIP_INFO:
                Map<String, Object> getShipInfoMap = new HashMap<>();
                getShipInfoMap.put("shipName", UserInfoManager.getInstance().userShipName(UserInfoMethod.GET, ""));
                getShipInfoMap.put("mmsi", "");
                sendMsgToH5(getShipInfoMap, "getShipInfo");
                break;
            case TYPE_GET_STATUS_BAR_HEIGHT:
                Map<String, Object> statusBarHeightMap = new HashMap<>();
                statusBarHeightMap.put("height", ScreenUtils.getStatusBarHeight(activity));
                sendMsgToH5(statusBarHeightMap, "getStatusBarHeight");
                break;
        }
    }

    /**
     * 发送消息给H5
     *
     * @param map
     * @param key
     */
    public void sendMsgToH5(Map<String, Object> map, String key) {
        final String jsonUserInfo = Utils.mapToJson(map);
        activity.runOnUiThread(() -> webView.loadUrl("javascript:$river." + key + ".success(" + jsonUserInfo + ")"));
    }
}
